import pygame
import sys
import shape as c_shape
import dqn as c_dqn


def game_over():
    pygame.quit()
    sys.exit()


def game_start():
    window_title = 'tetris'
    window_size = (200, 200)
    grid_num = (10, 10)  # 15列，32行
    grid_size = (20, 20)

    pygame.init()
    pg_clock = pygame.time.Clock()

    main_window = pygame.display.set_mode(window_size)
    pygame.display.set_caption(window_title)

    SHAPE = c_shape.SHAPE(grid_num)
    DQN = c_dqn.dqn()

    s = SHAPE.get_s()
    r = 0

    while True:

        a = DQN.choose_action(s)
        if a == 0:
            SHAPE.rota_shape()
        elif a == 1:
            SHAPE.move_step('left')
        elif a == 2:
            SHAPE.move_step('right')
        elif a == 3:
            pass

        SHAPE.move_step('down')

        r = SHAPE.get_r(r)
        print(r)
        s_ = SHAPE.get_s()

        DQN.store_ds(s, a, r, s_)
        if DQN.ds_st_learn == 1:
            DQN.learn()
        s = s_

        main_window.fill((0, 0, 0))
        SHAPE.draw_shape(main_window, grid_size, )
        pygame.display.update()
        # pg_clock.tick(5)


if __name__ == '__main__':
    game_start()
